package editManager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

import dbManage.LineItem;

public class Edit implements Serializable {
		
	private int cutNumber;
	private Date orderDate;
	private String customerID, po, managementCo, contact, editText;
	private String billLineOne, billLineTwo, billLineThree, billLineFour, shipLineOne, shipLineTwo, shipLineThree, shipLineFour;
	private double taxable, nonTax, tax, freight, labor, surcharge, misc, grossTotal;
	private ArrayList<LineItem> lineItems;
	private boolean over1K, creditCard, orderChange, sentToCredit;
	private String status;
	
	public Edit(String inputStream){
		
		over1K = false; creditCard = false; orderChange = false; sentToCredit = false;
		lineItems = new ArrayList<LineItem>();
			
		editText = inputStream;
		orderDate = new Date();
		Scanner scan = new Scanner(inputStream);
		int lineNum = 0;
				
		while(scan.hasNext()){
			String data = scan.nextLine();
			
			switch(lineNum){
				case 5:
					if(data.contains("****") && !data.contains("PREPAID") ){
						if(data.contains("ORDER CHANGED")){
							orderChange = true;
						}
						if(scan.nextLine().contains("S E N D   T O   C R E D I T")){
							sentToCredit = true;
						}
						lineNum = -1;
					}
					break;
				case 11:
					customerID = data.substring(10, 16);
					billLineOne = data.substring(21, 45);
					shipLineOne = data.substring(46);
					break;
				case 12:
					managementCo = data.substring(6, 9);
					billLineTwo = data.substring(21, 45);
					shipLineTwo = data.substring(46);
					break;
				case 13:
					billLineThree = data.substring(21, 45);
					shipLineThree = data.substring(46);
					break;
				case 14:
					int dLength = data.length();
					if(dLength>47){
						billLineFour = data.substring(21, 45);
						shipLineFour = data.substring(46);
						break;
					}
					if(dLength>21){
						billLineFour = data.substring(21);
						shipLineFour = "";
						break;
					}
					billLineFour = "";
					shipLineFour = "";
					break;
				case 18:
					try{cutNumber = Integer.parseInt(data.substring(5,11));} catch(Exception e){cutNumber = 0;}
					break;
				case 19:
					contact = data.substring(9, 24);
					break;
				case 21:
					boolean running = true;
					while(running){
						if(data.equals("")){
							scan.nextLine();
							running = false;
						}
						else{
							LineItem li = new LineItem(data, LineItem.EDIT);
							if(li.containsPO()){
								data = data.trim();
								data = data.replace("PO", "");
								if(data.charAt(0) == '#' || data.charAt(0) == ':'){
									po = data.substring(1,data.length());
								}
								else{
									po = data;
								}
							}
							data = scan.nextLine();
						}
					}
					break;
				case 24:
					try{taxable = Double.parseDouble(data.substring(54, 65));} catch(Exception e){taxable = 0;}
					break;
				case 25:
					try{nonTax = Double.parseDouble(data.substring(54,65));} catch(Exception e){nonTax = 0;}
					break;
				case 26:
					try{tax = Double.parseDouble(data.substring(54, 65));} catch(Exception e){tax = 0;}
					break;
				case 27:
					try{freight = Double.parseDouble(data.substring(54, 65));} catch(Exception e){freight = 0;}
					break;
				case 28:
					try{labor = Double.parseDouble(data.substring(54, 65));} catch(Exception e){labor = 0;}
					break;
				case 29: 
					try{surcharge = Double.parseDouble(data.substring(54, 65));} catch(Exception e){surcharge = 0;}
					break;
				case 30:
					try{misc = Double.parseDouble(data.substring(54, 65));} catch(Exception e){misc = 0;}
					break;
				case 31:
					try{grossTotal = Double.parseDouble(data.substring(54, 65));} catch(Exception e){grossTotal = 0;}
					break;
			}
			
			if(data.contains("CREDIT CARD")){
				creditCard = true;
			}
			lineNum++;
		}
		if(grossTotal > 1000){
			over1K = true;
		}
		scan.close();	
		
		if(sentToCredit){
			status = "Credit";
		}
		else{
			status = "In Work";
		}
		
	}

	public boolean isSentToCredit() {
		return sentToCredit;
	}

	public void setSentToCredit(boolean sentToCredit) {
		this.sentToCredit = sentToCredit;
	}
	
	public String getCustomerName(){
		return billLineOne;
	}

	public String getCustomerNumber() {
		return customerID;
	}

	public void setCustomerNumber(String customerNumber) {
		this.customerID = customerNumber;
	}

	public int getCutNumber() {
		return cutNumber;
	}

	public void setCutNumber(int cutNumber) {
		this.cutNumber = cutNumber;
	}

	public boolean isOver1K() {
		return over1K;
	}

	public void setOver1K(boolean over1k) {
		over1K = over1k;
	}

	public String getEditText() {
		return editText;
	}

	public void setEditText(String editText) {
		this.editText = editText;
	}

	public boolean isCreditCard() {
		return creditCard;
	}

	public void setCreditCard(boolean creditCard) {
		this.creditCard = creditCard;
	}

	public boolean isOrderChange() {
		return orderChange;
	}

	public void setOrderChange(boolean orderChange) {
		this.orderChange = orderChange;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	
	
}
